﻿using System;
using System.Web;
using System.Web.SessionState;
using SE6;
/// <summary>
/// 会话管理，所以会话时需要保存的信息都有该类统一管理
/// </summary>
public class SessionMan
{
	//生成验证码图片时，保存验证码在服务器，以便下次验证
	public static void SaveCheckCode(String checkCode)
	{
		Put(CHECK_CODE, checkCode);
	}
	//生成验证码图片时，保存验证码在服务器，以便下次验证
	public static Boolean ValidateCheckCode(String checkCode)
	{
		//return true;//TODO: 测试用
		String oldCC=Get<String>(CHECK_CODE);
		return checkCode != null && oldCC!=null && String.Compare(checkCode,oldCC , true) == 0;
	}
	//保存登录用户的状态，维持用户登录状态
	public static void SaveLoginUser(User usr)
	{
		Put(LOGIN_USER,usr);
	}
	public static User GetLoginUser()
	{
		return Get<User>(LOGIN_USER);
	}
	//保存结果，用于在页面间跳转时，传递一个字符串参数
	public static void SaveResult(String result)
	{
		Put(RESULT, result);
	}
	public static String GetResult()
	{
		String res = Get<String>(RESULT);
		Put(RESULT, null);
		return res;
	}
	//保存重置密码的用户
	public static void SaveResetPasswordUser(User usr)
	{
		Put(RESET_PASSWORD_USER, usr);
	}
	//返回重置密码的用户
	public static User GetResetPasswordUser()
	{
		return Get<User>(RESET_PASSWORD_USER);
	}

	private static void Put(String keyword,Object obj)
	{
		HttpContext.Current.Session[keyword] = obj;
	}
	private static T Get<T>(String keyword)
	{
		return (T)HttpContext.Current.Session[keyword];
	}
	private const String RESULT = "Result";
	private const String RESET_PASSWORD_USER = "RPUser";
	private const String LOGIN_USER = "LoginUser";
	private const String CHECK_CODE = "CheckCode";
}
